package UserInterface;

import java.awt.Color;

import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.text.MaskFormatter;

public class EnterMobileNumber extends JPanel {

	JButton btnBack = new JButton("<<Back");
	private String s="";
	MaskFormatter formatter;
	private JFormattedTextField textFieldMobileNumber;
	NumberPanel panelNumbers = new NumberPanel();
	void putNumbersListener(ActionListener list){
		panelNumbers.addActionListener(list);
	}
	EditorsPanel panelEditers = new EditorsPanel();
	public void putNumbers(){
		JButton[] buttons =panelNumbers.getButtons();
		for(int i=0;i<buttons.length;i++){
			buttons[i].addActionListener(new Listen(buttons[i].getText()));
		}
	}
	class Listen implements ActionListener{
		String text="";
		Listen(String text){
			this.text=text;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			if (s.length()<9)
			{
			s+=text;
			textFieldMobileNumber.setText(s);
			}
			
		}
		
	}
	
	public void clearS(){
		s="";
	}
	public String getS(){
		return s;
	}
	public void setS(String s){
		this.s=s;
		textFieldMobileNumber.setText(s);
	}

	public void putListenerOk(ActionListener list){
		panelEditers.setOkListener(list);
	}
	
	public void putListenerCancel(ActionListener list){
		panelEditers.setCancelListener(list);
	}
	
	public void putListenerChange(ActionListener list){
		panelEditers.setChangeListener(list);
	}
	
	public void putListenerBack(ActionListener list){
		btnBack.addActionListener(list);
	}

	/**
	 * Create the panel.
	 */
	public  EnterMobileNumber() {
		setBackground(new Color(255, 248, 220));
		this.setSize(500, 500);
		
		JLabel lblEnterMobileNumber = new JLabel("Enter  mobile number :");
		lblEnterMobileNumber.setForeground(new Color(128, 0, 0));
		lblEnterMobileNumber.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 18));
		
		try {
			formatter = new MaskFormatter("0#########");
			//formatter.setPlaceholderCharacter('_');
		   
			textFieldMobileNumber = new JFormattedTextField(formatter);
			textFieldMobileNumber.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 14));
			textFieldMobileNumber.setColumns(10);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		panelNumbers = new NumberPanel();
		
		 panelEditers = new EditorsPanel();
		
		
		btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnBack.setForeground(new Color(128, 0, 0));
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(142)
							.addComponent(lblEnterMobileNumber))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(114)
							.addComponent(textFieldMobileNumber, GroupLayout.PREFERRED_SIZE, 256, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(48)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(btnBack, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(panelNumbers, GroupLayout.PREFERRED_SIZE, 229, GroupLayout.PREFERRED_SIZE)
									.addGap(30)
									.addComponent(panelEditers, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)))))
					.addContainerGap(43, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(66)
					.addComponent(lblEnterMobileNumber)
					.addGap(18)
					.addComponent(textFieldMobileNumber, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
					.addGap(55)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(panelEditers, 0, 0, Short.MAX_VALUE)
						.addComponent(panelNumbers, GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE))
					.addGap(34)
					.addComponent(btnBack, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
					.addGap(41))
		);
		setLayout(groupLayout);

	}
}
